package lambda;

import com.alibaba.fastjson.JSON;
import lombok.Data;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.*;

/**
 * @author chi  2019-07-19 18:32
 **/
@Data
public class Test {

    private String department;
    private long timestamp = LocalDateTime.now().toEpochSecond(ZoneOffset.MAX);

    //getters and setters


    public static void main(String[] args) {

        List<Test> testList = new ArrayList<>();

        for (int i=1; i<10; i++) {
            Test test = new Test();
            test.setDepartment("sdlfkj"+i);

            testList.add(test);
        }

        Test test = new Test();
        test.setDepartment("sdlfkj1");
        testList.add(test);

        Map<String, List<Long>> map =
                testList.stream()
                        .collect(groupingBy(Test::getDepartment, HashMap::new,mapping(Test::getTimestamp, toList())));

        Map<String, List<Test>> collect = testList.stream().collect(groupingBy(Test::getDepartment, toList()));


        System.out.println(JSON.toJSONString(map));


        LongSummaryStatistics collect1 = testList.stream()

                .collect(summarizingLong(Test::getTimestamp));


        List<String> views = Arrays.asList("wsbs","xafaswzx","b8fw","ad");
       Map<Integer, Object> res = views.stream().collect(

                groupingBy(String::length,
                            Collectors.collectingAndThen(
                                Collectors.maxBy(Comparator.comparingInt(String::length)),
                                Optional::get  // 为转换函数，转换最终的数据
                            )
                    )

        );

   /*     Map<Integer, Long> collect2 = views.stream().collect(

                groupingBy(String::length,
                        counting()
                )

        );*/

        System.out.println(JSON.toJSONString(res));


    }

}